//
//  eduDatePickView.m
//  HappyStore
//
//  Created by 点点充 on 16/1/27.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "eduDatePickView.h"

@interface eduDatePickView ()
{
    CDatePickerViewEx *mainPick;
    UIButton *buttonOK;
    
    NSDate *userBirth;
    NSInteger selectedRow;
    
    UILabel *labShowTitle;
}

@end

@implementation eduDatePickView

+ (eduDatePickView *)sharedSelectedAddress
{
    static eduDatePickView *_sharedService = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedService = [[eduDatePickView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _sharedService.backgroundColor = [[UIColor colorWithWhite:0.6 alpha:1.0f] colorWithAlphaComponent:0.7];
    });
    return _sharedService;
}

- (void)btnClickDate
{
    self.hidden = NO;
    //加入手势来取消键盘
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
                                             initWithTarget:self action:@selector(dismissView)];
    tapRecognizer.cancelsTouchesInView = NO;
    [self addGestureRecognizer:tapRecognizer];
    
    selectedRow = 0;
    mainPick = [[CDatePickerViewEx alloc] initWithFrame: CGRectMake(0, [UIScreen mainScreen].bounds.size.height-240, 320, 240)];
//    [mainPick setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
    mainPick.backgroundColor = [UIColor colorWithRed:209/255.f green:212/255.f blue:218/255.f alpha:1.f];
    [mainPick awakeFromNib];
    
//    [mainPick addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
    [self addSubview: mainPick];
    
    
    UIView *viewBack = [[UIView alloc] initWithFrame:(CGRect){0,mainPick.frame.origin.y - 40,320,40}];
    viewBack.backgroundColor = [UIColor colorWithRed:235/255.f green:236/255.f blue:237/255.f alpha:1.f];
    [self addSubview:viewBack];
    
    labShowTitle = [[UILabel alloc] initWithFrame:(CGRect){0,5,320-70,30}];
    labShowTitle.textColor = [UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f];
    labShowTitle.font = [UIFont systemFontOfSize:13.5f];
    labShowTitle.textAlignment = NSTextAlignmentCenter;
    [viewBack addSubview:labShowTitle];
    
    
    buttonOK = [UIButton buttonWithType: UIButtonTypeCustom];
    [buttonOK setTitle: @"确  定" forState: UIControlStateNormal];
    [buttonOK setFrame: CGRectMake(320-60, 5, 50, 30)];
    [buttonOK setTintColor: [UIColor grayColor]];
    buttonOK.layer.cornerRadius = 5;
    [buttonOK setBackgroundColor:[UIColor colorWithRed:32/255.0f green:165/255.0f  blue:240/255.0f alpha:1.0f]];
    [buttonOK addTarget: self action: @selector(buttobClicked:) forControlEvents: UIControlEventTouchUpInside];
    buttonOK.titleLabel.font = [UIFont systemFontOfSize:15.f];
    [viewBack addSubview: buttonOK];
    
    [[[[UIApplication sharedApplication] windows] firstObject] addSubview:self];
    
}

- (void)buttobClicked:(UIButton *)sender
{
//    if (userBirth == nil) {
//        userBirth = [NSDate date];
//    }
//    
//    NSString *stringDate = [self stringFromDate:userBirth];
//    NSLog(@"选择的日期为:%@",stringDate);
//    
//    [self.delegatee seceltedDate:stringDate];
    
    NSInteger year = [mainPick selectedRowInComponent:0];
    NSInteger day = [mainPick selectedRowInComponent:1];
    
    NSLog(@"%@-%@",[mainPick titleForRow:year forComponent:0],[mainPick titleForRow:day forComponent:1]);
    NSString *stringDate = [NSString stringWithFormat:@"%@.%@",[mainPick titleForRow:year forComponent:0],[mainPick titleForRow:day forComponent:1]];
    [self.delegatee seceltedDate:stringDate];
}

- (void)dateChange:(id)sender
{
    UIDatePicker *control = (UIDatePicker *)sender;
    userBirth = control.date;
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
    labShowTitle.text = [dateFormatter stringFromDate:control.date];
    //    NSLog(@"时间为:%@",userBirth);
}
- (NSString *)stringFromDate:(NSDate *)date{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    [dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    return destDateString;
}

- (void)dismissView
{
    self.hidden = YES;
}

@end
